package org.qy.star_common.util;

import java.io.File;

/**
 * @author yinger
 * @description TODO
 * @date 2024/3/20 14:21
 **/
public class PathUtil {
    public static String getUserDir(String username) {
        return Constants.BROKER_SERVER_DIR + "/" + username;
    }

    public static String getVirtualHostDir(String username, String virtualHostName) {
        return getUserDir(username) + "/" + virtualHostName;
    }

    public static String getQueueDir(String username, String virtualHostName, String queueName) {
        return getVirtualHostDir(username, virtualHostName) + "/" + queueName;
    }

    public static String getQueueMessageDataPath(String username, String virtualHostName, String queueName) {
        return getQueueDir(username, virtualHostName, queueName) + "/" + Constants.MESSAGE_DATA_FILE_NAME + "." + Constants.DATA_SUFFIX;
    }

    public static String getTempQueueMessageDataPath(String username, String virtualHostName, String queueName) {
        return getQueueDir(username, virtualHostName, queueName) + "/" + Constants.MESSAGE_TEMP_FILE_NAME + "." + Constants.DATA_SUFFIX;
    }

    public static boolean createBrokerDir() {
        File file = new File(Constants.BROKER_SERVER_DIR);
        boolean ok = false;
        if (!file.exists()) {
            ok = file.mkdirs();
            if (!ok) {
                return ok;
            }
        }

        if (!ok) {
            return ok;
        }

        return true;
    }

    public static boolean createUserDir(String username) {
        String userDir = getUserDir(username);
        File file = new File(userDir);
        boolean ok = false;
        if (!file.exists()) {
            ok = file.mkdirs();
            if (!ok) {
                return ok;
            }
        }

        if (!ok) {
            return ok;
        }

        return true;
    }

    public static boolean createVirtualHostDir(String username, String virtualHostName) {
        String virtualHostDir = getVirtualHostDir(username, virtualHostName);
        File file = new File(virtualHostDir);
        boolean ok = false;
        if (!file.exists()) {
            ok = file.mkdirs();
            if (!ok) {
                return ok;
            }
        }

        if (!ok) {
            return ok;
        }

        return true;
    }

    public static boolean createQueueDir(String username, String virtualHostName, String queueName) {
        String queueDir = getQueueDir(username, virtualHostName, queueName);
        File file = new File(queueDir);
        boolean ok = false;
        if (!file.exists()) {
            ok = file.mkdirs();
            if (!ok) {
                return ok;
            }
        }

        if (!ok) {
            return ok;
        }
        return true;
    }
}
